package com.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.Address;
import com.shop.entity.China;
import com.shop.entity.User;
import com.shop.service.AddressService;
import com.shop.service.ChinaService;
import com.shop.service.UserService;
import com.shop.util.UserTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author D
 * @since 2023-03-11 03:01:17
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private ChinaService chinaService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;
    /**
     * 根据父级ID查询中国行政区划列表
     * @param pid 父级ID
     * @return 包含行政区划信息的列表
     */
    @RequestMapping("/listByPid")
    public List<China> listByPid(Integer pid) {
        QueryWrapper<China> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Pid", pid);
        return chinaService.list(queryWrapper);
    }
    /**
     * 添加地址信息
     * @param address 要添加的地址信息
     * @param request HTTP请求对象
     */
    @RequestMapping("/add")
    public void add(Address address, HttpServletRequest request){
        User user = UserTokenUtil.getUser(request, userService);
        addressService.addAddress(address,user.getId());
    }
    /**
     * 根据用户ID获取地址列表
     * @param request HTTP请求对象
     * @return 包含地址信息的列表
     */
    @RequestMapping("/getList")
    public List<Address> getList(HttpServletRequest request){
        User user = UserTokenUtil.getUser(request, userService);
        return addressService.getListByUserId(user.getId());
    }

}

